package org.springbus.ff.util;

import com.google.common.collect.Lists;
import org.springbus.ff.conf.FFBaseConf;
import org.springbus.ff.core.FFCommand;

import java.util.ArrayList;
import java.util.List;

public class FFmpegUtil {

    public static FFCommand createCommand(int threads) {
        FFCommand cmd = new FFCommand();

        return cmd;
    }

    public static FFCommand addDefaultOptions(FFCommand command, FFBaseConf conf,
                                              boolean audio) {
        String vb = null;
        String crf = "20";
        String preset = "medium";
        String vprofile = "main";
        boolean upStreaming = conf.isUpStreaming();

        List<String> outputOptions = new ArrayList<>();
        //---- misc ----
        outputOptions.addAll(Lists.newArrayList(
                "-hide_banner", // hide_banner - parameter, you can display only meta information
                "-map_metadata",
                "-1",
                "-map_chapters",
                "-1"));

        //---- video ----
        outputOptions.addAll(Lists.newArrayList(
                "-c:v",
                "libx264", // c:v - H.264
                "-profile:v",
                vprofile, // profile:v - main profile: mainstream image quality. Provide I / P / B frames, default
                "-preset",
                preset, // preset - compromised encoding speed
                "-crf",
                crf, // crf - The range of quantization ratio is 0 ~ 51, where 0 is lossless mode, 23 is the default value, 51 may be the worst
                "-movflags",
                "faststart",
                "-pix_fmt",
                "yuv420p"
        ));

        //---- vb -----
        if (vb != null) {
            outputOptions.addAll(Lists.newArrayList("-vb", vb));
        }

        //---- audio ----
        if (audio) {
            outputOptions.addAll(Lists.newArrayList("-c:a", "copy", "-shortest"));
        }

        //---- live stream ----
        if (!upStreaming) {
            outputOptions.addAll(Lists.newArrayList("-map", "0"));
        }
        command.outputOptions(outputOptions);
        return command;
    }
}
